/**  
* @Title: ExceptionTool.java  
* @Package com.clps.dev.sms.util.object  
* @Description: ExceptionTool.java  
* @author RuiShan.Yang  
* @date 2018-11-28 09:20:58 
* @version V1.0  
*/

package com.clps.dev.sms.util.object;

import org.apache.log4j.Logger;

import com.clps.dev.sms.common.result.ResultType;

/**  
* @ClassName: ExceptionTool  
* @Description: 异常处理工具 
* @since JDK 1.8 
* @author RuiShan.Yang  
* @date 2018-11-28 09:20:58  
* 
*/

public class ExceptionTool {

	/**  
	* 创建一个新的实例 ExceptionTool.  
	*
	*/
	private Throwable exception;
	private Logger logger = Logger.getLogger(Logger.class);

	public ExceptionTool(Throwable exception) {
		this.exception = exception;
	}

	/**  
	* @Title: showTraces  
	* @Description:找到底层 错误
	* @param @return    参数  
	* @return Throwable    返回类型  
	* @throws  
	*/
	public Throwable showTraces() {
		Throwable next = exception.getCause();
		while (next != null) {
			exception = next;
			next = next.getCause();
		}
		return exception;
	}

	public String exceptionVal() {
		Throwable throwable = showTraces();
		// 打印出错信息
		logger.info(throwable.getMessage());
		if (throwable.getMessage().contains("cannot be null")) {
			// 不能为空值
			return ResultType.INPUT_NULL;
		}
		// 唯一性约束
		if (throwable.getMessage().contains("Duplicate entry")) {
			return ResultType.CONTENT_REPEAT;
		}
		return ResultType.ADD_FAULT;
	}

}
